import { Inject, Post, Body, Get, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { AppointmentInfoEntity } from '../../entity/info';
import { AppointmentInfoService } from '../../service/info';
import { UserInfoEntity } from '../../../user/entity/info';
import { OrderShopEntity } from '../../../order/entity/shop';

/**
 * 预约信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: AppointmentInfoEntity,
  service: AppointmentInfoService,
  pageQueryOp: {
    fieldEq: [
      'a.userId',
      'appointmentStatus',
      'a.comeType',
      'a.shopId',
      'c.registAdminId',
      'c.registChannelId',
    ],
    fieldLike: [],
    select: [
      'a.*',
      'c.nickName as userName',
      'c.phone as userPhone',
      'c.registChannelId as userChannelId',
      'c.registAdminId as userAdminId',
      'c.gender as userGender',
      'd.name as shopName',
      'c.remark as userRemark',
    ],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'c',
        condition: 'a.studentId = c.id',
        type: 'leftJoin',
      },
      {
        entity: OrderShopEntity,
        alias: 'd',
        condition: 'a.shopId = d.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const {
        source,
        appointmentTimeStartTime,
        appointmentTimeEndTime,
        phone,
        name,
      } = ctx.request.body;
      const where = [];
      if (appointmentTimeStartTime && appointmentTimeEndTime) {
        where.push([
          'a.appointmentTime BETWEEN :startTime AND :endTime',
          {
            startTime: appointmentTimeStartTime,
            endTime: appointmentTimeEndTime,
          },
        ]);
      }

      if (source) {
        where.push(['c.registChannelId = :source', { source }]);
      }

      if (phone) {
        where.push(['c.phone = :phone', { phone }]);
      }

      if (name) {
        where.push(['c.nickName = :name', { name }]);
      }

      return where;
    },
  },
})
export class AdminAppointmentInfoController extends BaseController {
  @Inject()
  appointmentInfoService: AppointmentInfoService;

  @Post('/appoint', { summary: '预约' })
  async appoint(
    @Body()
    param: {
      studentId: number;
      doctorId: number;
      shopId: number;
      date: string;
      time: string;
    }
  ) {
    const result = await this.appointmentInfoService.appoint(param);
    return this.ok(result);
  }

  // 取消预约
  @Post('/cancelAppoint', { summary: '取消预约' })
  async cancelAppoint(@Body() param: { appointId: number }) {
    const result = await this.appointmentInfoService.appointCancle(param);
    return this.ok(result);
  }

  @Get('/dateAppoint', { summary: '查看预约' })
  async dateAppoint(
    @Query()
    param: {
      doctorId: number;
      shopId: number;
      date: string;
      endDate?: string;
    }
  ) {
    // console.log('dateAppoint', param);
    const result = await this.appointmentInfoService.dateAppoint(param);
    return this.ok(result);
  }

  @Post('/appointCome', { summary: '预约到店' })
  async appointCome(@Body() param: { appointId: number }) {
    const result = await this.appointmentInfoService.appointCome(param);
    return this.ok(result);
  }

  // 限制门店星期的预约人数
  @Post('/updateShopWeekLimit', { summary: '限制门店星期的预约人数' })
  async updateShopWeekLimit(
    @Body() param: { shopId: number; week: number; limit: number }
  ) {
    const result = await this.appointmentInfoService.updateShopWeekLimit(param);
    return this.ok(result);
  }

  // 获取门店限制的星期数组
  @Get('/getShopWeekLimit', { summary: '获取门店限制的星期数组' })
  async getShopWeekLimit(@Query() param: { shopId: number }) {
    const result = await this.appointmentInfoService.getShopWeekLimit(
      param.shopId
    );
    return this.ok(result);
  }
}
